//
//  JXOrderViewController.m
//  JiaoX
//
//  Created by yinqy on 15/1/15.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXOrderViewController.h"
#import "JXOrderListController.m"

#define TITLE_BTN_HEIGHT 45

@interface JXOrderViewController ()<UIScrollViewDelegate>
{
    UIButton *validBtn;
    UIButton *succeBtn;
    UIButton *invalidBtn;
    
    UIImageView *_accessView;
    
    UIButton *_currentBtn;
}
@property(nonatomic,strong)NSMutableArray *childController;
@property(nonatomic,weak)UIScrollView *scrollView;

@end

@implementation JXOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"我的订单"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _childController = [NSMutableArray array];
    [self initView];
}

-(void)initView{
    _accessView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_order_access"]];
    
    
    validBtn = [self getTitleBtnWithTitle:@"有效" point:CGPointMake(0, NavHeight)];
    [validBtn setTag:100];
    [self titleBtnClick:validBtn];
    succeBtn = [self getTitleBtnWithTitle:@"交易完成" point:CGPointMake(JXScreenW/3,NavHeight)];
    [succeBtn setTag:101];
    invalidBtn = [self getTitleBtnWithTitle:@"无效" point:CGPointMake(JXScreenW/3*2,NavHeight)];
    [invalidBtn setTag:102];
    
    [self.view addSubview:validBtn];
    [self.view addSubview:succeBtn];
    [self.view addSubview:invalidBtn];
    
    
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(validBtn.frame), JXScreenW, self.view.height-NavHeight-TITLE_BTN_HEIGHT)];
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    
    for (int i=0; i<3; i++) {
        JXOrderListController *ctr = [JXOrderListController getInstanceWithOrderType:[NSString stringWithFormat:@"%d",i==2?-1:i+1]];
        [ctr.view setFrame:CGRectMake(i*JXScreenW, 0, JXScreenW, _scrollView.height)];
        [_childController addObject:ctr];
        [_scrollView addSubview:ctr.view];
    }
    [_scrollView setContentSize:CGSizeMake(JXScreenW*3, _scrollView.height)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
}

-(UIButton *)getTitleBtnWithTitle:(NSString *)title point:(CGPoint)point{
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor whiteColor]];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
    [btn setTitleColor:JXThemeColor forState:UIControlStateSelected];
    [btn setTitleColor:JXColor(77, 77, 77) forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(point.x, point.y, JXScreenW/3, TITLE_BTN_HEIGHT)];
    [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, TITLE_BTN_HEIGHT-0.5, JXScreenW/3, 0.5)];
    [view setBackgroundColor:JXColor(204, 204, 204)];
    
    [btn addSubview:view];
    return btn;
}

-(void)titleBtnClick:(UIButton *)btn{
    if(_currentBtn.tag ==btn.tag){
        return;
    }else{
        btn.selected = YES;
        _currentBtn.selected = false;
        _currentBtn = btn;
        [self setScrollCurrentPageNum:(int)btn.tag-100];
        
        [_accessView removeFromSuperview];
        _accessView.y = TITLE_BTN_HEIGHT-_accessView.height;
        _accessView.x = (JXScreenW/3-_accessView.width)/2;
        [btn addSubview:_accessView];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当前页数
    int pageNum = (scrollView.contentOffset.x+scrollView.width*0.5)/scrollView.width;
    [self setScrollCurrentPageNum:pageNum];
    UIButton *btn = (UIButton *)[self.view viewWithTag:pageNum+100];
    [self titleBtnClick:btn];
}


-(void)setScrollCurrentPageNum:(int)pageNum{
    CGPoint p = CGPointMake(pageNum*_scrollView.frame.size.width, 0);
    [_scrollView setContentOffset:p];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [_childController makeObjectsPerformSelector:@selector(requestData)];
    
}

-(BOOL)shouldLogin{
    return YES;
}
@end
